﻿using System;
using System.Drawing;
using System.Web.UI.WebControls;
using System.Collections.Generic;

public partial class Pages_Carrier_TripEdit : System.Web.UI.Page
{
	Int64 Trip_id { get { return DS.ToInt64(Request.QueryString["Id"]); } }

	protected void Page_Load(object sender, EventArgs e)
	{
		if (!IsPostBack)
		{
			if ((Session["TripSaved"] ?? string.Empty).ToString() == "SavedGood")
			{
				lbMessage.ForeColor = Color.Green;
				lbMessage.Text = "Данные сохранены успешно";
				Session["TripSaved"] = null;
			}
			if (Trip_id > 0)
				InitUpdateContext();
		}
	}

	private void InitUpdateContext()
	{
		Trip tr = DBEntity<Trip>.Load(Trip_id);
		if (tr == null)
			Response.Redirect("~/");
		lbBusName.Text = tr.bus.Name;
		hfBusID.Value = tr.Bus_id.ToString();
		lbRouteName.Text = tr.route.Name;
		hfRouteID.Value = tr.Route_id.ToString();
		tbTripTime.Text = tr.dateTime.ToShortTimeString();
		tbTripDT.Text = tr.dateTime.ToShortDateString();
	}

	protected void OnEditSearch(object s, EventArgs e)
	{
		hfModalAction.Value = (s as LinkButton).CommandName;
		tbSearch.Text = string.Empty;
		rptSearch.DataSource = null;
		rptSearch.DataBind();
		lkbAddSearch.Visible = false;
		MPE.Show();
	}

	protected void OnTripSave(object s, EventArgs e)
	{
		if (IsValid)
		{
			Trip tr = DBEntity<Trip>.Load(Trip_id) ?? new Trip();
			tr.bus.Id = DS.ToInt64(hfBusID.Value);
			tr.route.Id = DS.ToInt64(hfRouteID.Value);
			
			DateTime TripTime = DS.ToDateTime(tbTripTime.Text);
			tr.dateTime = DS.ToDateTime(tbTripDT.Text);
			tr.dateTime = tr.dateTime.AddHours(TripTime.Hour);
			tr.dateTime = tr.dateTime.AddMinutes(TripTime.Minute);

			if (tr.Merge())
			{
				if (Trip_id == 0)
				{
					Session["TripSaved"] = "SavedGood";
					Response.Redirect("~/Pages/Carrier/TripEdit.aspx?Id=" + tr.Id.ToString());
				}
				else
				{
					lbMessage.ForeColor = Color.Green;
					lbMessage.Text = "Данные сохранены успешно";
				}
			}
			else
			{
				lbMessage.ForeColor = Color.Red;
				lbMessage.Text = "При сохранении данных проихошла ошибка";
			}
		}
	}

	protected void OnCancel(object s, EventArgs e)
	{
		Response.Redirect("~/Pages/Carrier/Trip.aspx");
	}

	protected void OnSearch(object s, EventArgs e)
	{
		if (hfModalAction.Value == "Bus")
		{
			Bus b = new Bus();
			b.Name = tbSearch.Text;
			rptSearch.DataSource = DBEntity<Bus>.Find(b);
			lkbAddSearch.Visible = ((rptSearch.DataSource as IList<Bus>).Count == 0);
		}
		if (hfModalAction.Value == "Route")
		{
			Route r = new Route();
			r.Name = tbSearch.Text;
			rptSearch.DataSource = DBEntity<Route>.Find(r);
			lkbAddSearch.Visible = ((rptSearch.DataSource as IList<Route>).Count == 0);
		}
		rptSearch.DataBind();
	}

	protected void OnAddSearch(object s, EventArgs e)
	{
		if (hfModalAction.Value == "Bus")
		{
			Bus b = new Bus();
			b.Name = tbSearch.Text;
			b.PlaceCount = 1;
			if (b.Merge())
			{
				lbBusName.Text = b.Name;
				hfBusID.Value = b.Id.ToString();
			}
		}
		if (hfModalAction.Value == "Route")
		{
			Route r = new Route();
			r.Name = tbSearch.Text;
			r.SourceStation.Id = 1;
			r.TargetStation.Id = 1;
			if (r.Merge())
			{
				lbRouteName.Text = r.Name;
				hfRouteID.Value = r.Id.ToString();
			}
		}
		upRouteBus.Update();
	}

	protected void OnApplySearch(object s, EventArgs e)
	{
		if (hfModalAction.Value == "Bus")
		{
			lbBusName.Text = (s as LinkButton).Text;
			hfBusID.Value = (s as LinkButton).CommandArgument;
		}
		if (hfModalAction.Value == "Route")
		{
			lbRouteName.Text = (s as LinkButton).Text;
			hfRouteID.Value = (s as LinkButton).CommandArgument;
		}
		upRouteBus.Update();
	}
}
